'''
Created on 2009-7-20

@author: roy
'''

import re
import sys

from logMessage import *

def encode_str(str, format=None):
    """Format the string with coded.
    
    Arguments:
    
    str - string to code. 
    format - the coding format you want. Default is the system coding.
    """
    return str.decode('utf-8')
#===============================================================================
#    if format:
#        return str.decode('UTF-8').encode(format) 
#    else:
#        return str.decode('UTF-8').encode(sys.getfilesystemencoding()) 
#===============================================================================


def search_text(pattern, src_str, group_match=False, multiline=True):
    '''Search text with the regular expression.
    
    If the group_match argument was true which the result of tuple consist of 
    one or more than one lists.
    
    Arguments:
    
    pattern - regular pattern
    src_str - the string to find within pattern
    group_match - whether search used group
    multiline - search in multi-line mode
    '''
    result_list = list()
 
    if multiline:
        match_list = list(re.finditer(pattern, src_str, re.S))
    else:
        match_list = list(re.finditer(pattern, src_str))
    if len(match_list) > 0:
        for match in match_list:
            if not group_match:
                result_list.append(match.group())
            else:
                tmp_list = list()
                for index in range(get_group_size(pattern)):
                    tmp_list.append(match.group(index + 1))
                    pass
                result_list.append(tmp_list)
    return tuple(result_list)


def get_group_size(src_str):
    '''Count no leading backslash parenthesis of the pattern.
    
     Arguments:
     
     src_str - the pattern
     '''
    pattern = '(\\\\)?\('
    count = 0 
    for match in re.finditer(pattern, src_str):
        if not match.group(0).startswith('\\'):
            count = count + 1
    return count


def validCookie(cookie):
    if cookie != None:
        if (cookie != '') and (cookie.find('_user=deleted')<0) and (cookie.find('_user=')>=0):
            return True
    return False
    

def log(txt):
    #print txt
    #pass
    addLog(txt)


def removeOldLog():
    deleteOld()
    #pass


def debug(txt):
    #print txt
    pass



